// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Ring Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 10 : TYPE_RING

// Function declarations
reserve function Ring_DebugDraw
reserve function Ring_DebugSpawn

// Static Values

// Tables

function Ring_DebugDraw
	DrawSprite(0)
end function


function Ring_DebugSpawn
	CreateTempObject(TypeName[Ring], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
end function


event ObjectMain
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(object[0].state, PlayerObject_Knockback)
		temp0 = checkResult
		CheckEqual(object[currentPlayer].state, PlayerObject_Knockback)
		temp0 = checkResult
		CheckEqual(object[currentPlayer].state, PlayerObject_Hurt)
		temp0 |= checkResult
		if temp0 == 0
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0	
				if object.type == TypeName[Ring]
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						object.type = TypeName[Ring Sparkle]
						if stage.debugMode == 0
							saveRAM[997]++
							if saveRAM[997] > saveRAM[998]
								saveRAM[998] = saveRAM[997]
							end if
						end if
						object[0].value0++
						if object[0].value0 > 999
							object[0].value0 = 999
						end if
						if object[0].value0 >= ringExtraLife
							if options.gameMode < 2
								if saved.lives == 0
									player.lives++
									if PleaseReplaceLifeSoundEffect == 1
										PlaySfx(SfxName[LifeDDLC], 0)
									else	
										PlaySfx(SfxName[Life], 0)
									end if			
									PauseMusic()
									ResetObjectEntity(25, TypeName[Music Event], 2, 0, 0)
									object[25].priority = PRIORITY_ACTIVE
								end if
							end if
							if stage.debugMode == 0
								if object[0].value0 >= 200
									achievement.trigger = 6
									//CallNativeFunction2(SetAchievement, 4, 100)
								end if
							end if
							ringExtraLife += 100
							if ringExtraLife > 300
								ringExtraLife = 1000
							end if
						end if
						if object.propertyValue == 1
							if stage.debugMode == 0
								if options.attractMode == 0
									if currentPlayer == 0
										if object[0].animation == ANI_JUMPING
											achieveRingCount++
											if achieveRingCount == 30
												//CallNativeFunction2(SetAchievement, 0, 100)
												achievement.trigger = 2
												achieveRingCount = 0
											end if
										end if
									end if
								end if
							end if
						end if
						if ringPan == 0
							PlaySfx(SfxName[Ring L], 0)
							SetSfxAttributes(SfxName[Ring L], -1, -100)
							ringPan = 1
						else
							PlaySfx(SfxName[Ring R], 0)
							SetSfxAttributes(SfxName[Ring R], -1, 100)
							ringPan = 0
						end if
					else
						if object.state == 0
							if object[currentPlayer].value37 == 4
								BoxCollisionTest(C_TOUCH, object.entityPos, -64, -64, 64, 64, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
								if checkResult == 1
									object.state = 1
									object.value1 = currentPlayer
								end if
							end if
						end if
					end if
				end if
			end if
		end if
	next
	if object.state == 1
		arrayPos0 = object.value1
		if object[arrayPos0].value37 != 4
			object.type = TypeName[Lose Ring]
			object.animationSpeed = 128
			object.alpha = 256
		else
			arrayPos0 = object.value1
			if object.xpos > object[arrayPos0].xpos
				if object.xvel > 0
					object.xvel -= 0xC000
				else
					object.xvel -= 0x3000
				end if
			else
				if object.xvel < 0
					object.xvel += 0xC000
				else
					object.xvel += 0x3000
				end if
			end if
			if object.ypos > object[arrayPos0].ypos
				if object.yvel > 0
					object.yvel -= 0xC000
				else
					object.yvel -= 0x3000
				end if
			else
				if object.yvel < 0
					object.yvel += 0xC000
				else
					object.yvel += 0x3000
				end if
			end if
			object.xpos += object.xvel
			object.ypos += object.yvel
		end if
	end if
end event


event ObjectDraw
	DrawSprite(ringFrame)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")
if stage.playerListPos == 0
	if stage.player2Enabled == 0
   		 foreach(TypeName[Ring], arrayPos0, ALL_ENTITIES)
       			 if object[arrayPos0].propertyValue == 2
            				ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		
       			 end if
    		next
	end if
end if
if stage.playerListPos == 5
	if stage.player2Enabled == 0
   		 foreach(TypeName[Ring], arrayPos0, ALL_ENTITIES)
       			 if object[arrayPos0].propertyValue == 2
            				ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		
       			 end if
    		next
	end if
end if
	foreach (TypeName[Ring], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next
	achieveRingCount = 0
	SpriteFrame(-8, -8, 16, 16, 1, 1)
	SpriteFrame(-8, -8, 16, 16, 1, 18)
	SpriteFrame(-8, -8, 16, 16, 1, 35)
	SpriteFrame(-8, -8, 16, 16, 1, 52)
	SpriteFrame(-8, -8, 16, 16, 1, 69)
	SpriteFrame(-8, -8, 16, 16, 1, 86)
	SpriteFrame(-8, -8, 16, 16, 1, 103)
	SpriteFrame(-8, -8, 16, 16, 1, 120)
	SetTableValue(TypeName[Ring], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Ring_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Ring_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-8,-8,16,16,1,1)
end event
